package com.cencat.common.enums;

/**
 * 错误码枚举
 * 统一管理系统中的所有错误码
 * 
 * 错误码规范：
 * - 200: 成功
 * - 400-499: 客户端错误
 * - 500-599: 服务端错误
 * - 1000-1999: 参数校验错误
 * - 2000-2999: 用户权限相关错误
 * - 3000-3999: 商家相关错误
 * - 4000-4999: 产品相关错误
 * - 5000-5999: 司机相关错误
 * - 6000-6999: 订单相关错误
 * - 7000-7999: 派单相关错误
 * - 8000-8999: 任务相关错误
 * - 9000-9999: 路线相关错误
 * - 10000+: 其他业务错误
 * 
 * @author cencat
 * @since 2024-01-01
 */
public enum ErrorCode {
    
    // ==================== 通用成功码 ====================
    SUCCESS(200, "操作成功"),
    
    // ==================== 通用客户端错误码 (400-499) ====================
    PARAM_ERROR(400, "参数错误"),
    UNAUTHORIZED(401, "未授权访问"),
    FORBIDDEN(403, "禁止访问"),
    NOT_FOUND(404, "资源不存在"),
    METHOD_NOT_ALLOWED(405, "请求方法不允许"),
    CONFLICT(409, "资源冲突"),
    
    // ==================== 通用服务端错误码 (500-599) ====================
    SYSTEM_ERROR(500, "系统内部错误"),
    INTERNAL_SERVER_ERROR(500, "系统内部错误"),
    OPERATION_FAILED(501, "操作失败"),
    OPERATION_ERROR(502, "操作异常"),
    DATABASE_ERROR(503, "数据库错误"),
    NETWORK_ERROR(504, "网络错误"),
    TIMEOUT_ERROR(505, "请求超时"),
    SERVICE_UNAVAILABLE(506, "服务不可用"),
    FUNCTION_NOT_IMPLEMENTED(507, "功能未实现"),
    
    // ==================== 参数校验错误码 (1000-1099) ====================
    PARAM_INVALID(1000, "参数无效"),
    PARAM_MISSING(1001, "参数缺失"),
    PARAM_FORMAT_ERROR(1002, "参数格式错误"),
    PARAM_LENGTH_ERROR(1003, "参数长度错误"),
    PARAM_RANGE_ERROR(1004, "参数范围错误"),
    PARAM_TYPE_ERROR(1005, "参数类型错误"),
    PARAM_IS_NULL(1006, "参数不能为空"),
    PARAM_IS_BLANK(1007, "参数不能为空字符串"),
    PARAM_REGEX_ERROR(1008, "参数格式不匹配"),
    PARAM_ENUM_ERROR(1009, "参数枚举值错误"),
  
    
    // ==================== 用户相关错误码 (2000-2099) ====================
    USER_NOT_EXISTS(2000, "用户不存在"),
    USER_ALREADY_EXISTS(2001, "用户已存在"),
    USER_STATUS_ERROR(2002, "用户状态异常"),
    USER_DISABLED(2003, "用户已禁用"),
    USER_LOCKED(2004, "用户已锁定"),
    USERNAME_EXISTS(2005, "用户名已存在"),
    EMAIL_ALREADY_EXISTS(2006, "邮箱已存在"),
    PHONE_ALREADY_EXISTS(2007, "手机号已存在"),
    PASSWORD_ERROR(2008, "密码错误"),
    OLD_PASSWORD_ERROR(2009, "原密码错误"),
    PASSWORD_WEAK(2010, "密码强度不够"),
    LOGIN_FAILED(2011, "登录失败"),
    LOGIN_EXPIRED(2012, "登录已过期"),
    TOKEN_INVALID(2013, "令牌无效"),
    TOKEN_EXPIRED(2014, "令牌已过期"),
    USER_PERMISSION_DENIED(2015, "用户权限不足"),
    USER_ACCOUNT_LOCKED(2016, "用户账户已锁定"),
    USER_NOT_FOUND(2017, "用户未找到"),
    USER_REGISTER_FAILED(2018, "用户注册失败"),
    USER_LOGIN_FAILED(2019, "用户登录失败"),
    USER_LOGOUT_FAILED(2020, "用户登出失败"),
    USER_UPDATE_FAILED(2021, "用户更新失败"),
    USER_DELETE_FAILED(2022, "用户删除失败"),
    USER_INFO_INCOMPLETE(2023, "用户信息不完整"),
    USER_VERIFICATION_FAILED(2024, "用户验证失败"),
    USER_ACTIVATION_FAILED(2025, "用户激活失败"),
    USER_DEACTIVATION_FAILED(2026, "用户停用失败"),
    USER_ROLE_ASSIGNMENT_FAILED(2027, "用户角色分配失败"),
    USER_PERMISSION_UPDATE_FAILED(2028, "用户权限更新失败"),
    
    // ==================== 角色相关错误码 (2100-2199) ====================
    ROLE_NOT_EXISTS(2100, "角色不存在"),
    ROLE_NOT_FOUND(2100, "角色不存在"),
    ROLE_ALREADY_EXISTS(2101, "角色已存在"),
    ROLE_CODE_EXISTS(2102, "角色编码已存在"),
    ROLE_NAME_EXISTS(2103, "角色名称已存在"),
    ROLE_HAS_USERS(2104, "角色下存在用户，无法删除"),
    ROLE_DISABLED(2105, "角色已禁用"),
    ROLE_STATUS_ERROR(2106, "角色状态错误"),
    ROLE_PERMISSION_DENIED(2107, "角色权限不足"),
    
    // ==================== 权限相关错误码 (2200-2299) ====================
    PERMISSION_NOT_EXISTS(2200, "权限不存在"),
    PERMISSION_NOT_FOUND(2200, "权限不存在"),
    PERMISSION_ALREADY_EXISTS(2201, "权限已存在"),
    PERMISSION_CODE_EXISTS(2202, "权限编码已存在"),
    PERMISSION_NAME_EXISTS(2203, "权限名称已存在"),
    PERMISSION_DENIED(2204, "权限不足"),
    PERMISSION_DISABLED(2205, "权限已禁用"),
    PERMISSION_STATUS_ERROR(2206, "权限状态错误"),
    
    // ==================== 商家相关错误码 (3000-3099) ====================
    MERCHANT_NOT_EXISTS(3000, "商家不存在"),
    MERCHANT_ALREADY_EXISTS(3001, "商家已存在"),
    MERCHANT_CODE_EXISTS(3002, "商家编码已存在"),
    MERCHANT_NAME_EXISTS(3003, "商家名称已存在"),
    MERCHANT_DISABLED(3004, "商家已禁用"),
    MERCHANT_LOCKED(3005, "商家已锁定"),
    MERCHANT_AUDIT_PENDING(3006, "商家审核中"),
    MERCHANT_AUDIT_REJECTED(3007, "商家审核被拒绝"),
    CONTACT_PHONE_EXISTS(3008, "联系电话已存在"),
    CREDIT_CODE_EXISTS(3009, "统一社会信用代码已存在"),
    MERCHANT_STATUS_ERROR(3010, "商家状态错误"),
    MERCHANT_NOT_APPROVED(3011, "商家未审核通过"),
    MERCHANT_EXPIRED(3012, "商家已过期"),
    MERCHANT_SUSPENDED(3013, "商家已暂停"),
    BUSINESS_LICENSE_EXISTS(3014, "营业执照已存在"),
    CONTACT_EMAIL_EXISTS(3015, "联系邮箱已存在"),
    MERCHANT_LOCATION_ERROR(3016, "商家位置信息错误"),
    MERCHANT_BINDING_ERROR(3017, "商家绑定关系错误"),
    
    // ==================== 产品相关错误码 (4000-4099) ====================
    PRODUCT_NOT_EXISTS(4000, "产品不存在"),
    PRODUCT_ALREADY_EXISTS(4001, "产品已存在"),
    PRODUCT_CODE_EXISTS(4002, "产品编码已存在"),
    PRODUCT_NAME_EXISTS(4003, "产品名称已存在"),
    PRODUCT_DISABLED(4004, "产品已禁用"),
    PRODUCT_OUT_OF_STOCK(4005, "产品库存不足"),
    PRODUCT_ALREADY_ON_SHELF(4006, "产品已上架"),
    PRODUCT_ALREADY_OFF_SHELF(4007, "产品已下架"),
    PRODUCT_NOT_APPROVED(4008, "产品未审核通过"),
    INSUFFICIENT_STOCK(4009, "库存不足"),
    PRODUCT_STATUS_ERROR(4010, "产品状态错误"),
    PRODUCT_CATEGORY_NOT_EXISTS(4011, "产品分类不存在"),
    PRODUCT_BRAND_NOT_EXISTS(4012, "产品品牌不存在"),
    PRODUCT_PRICE_ERROR(4013, "产品价格错误"),
    PRODUCT_EXPIRED(4014, "产品已过期"),
    PRODUCT_SOLD_OUT(4015, "产品已售罄"),
    PRODUCT_STOCK_NOT_ENOUGH(4016, "产品库存不足"),
    
    // ==================== 司机相关错误码 (5000-5099) ====================
    DRIVER_NOT_EXISTS(5000, "司机不存在"),
    DRIVER_ALREADY_EXISTS(5001, "司机已存在"),
    DRIVER_NOT_ACTIVE(5002, "司机未激活"),
    DRIVER_DISABLED(5003, "司机已禁用"),
    DRIVER_BUSY(5004, "司机忙碌中"),
    DRIVER_LICENSE_INVALID(5005, "驾驶证无效"),
    DRIVER_LICENSE_EXPIRED(5006, "驾驶证已过期"),
    DRIVER_STATUS_ERROR(5007, "司机状态错误"),
    DRIVER_NOT_AVAILABLE(5008, "司机不可用"),
    DRIVER_LOCATION_ERROR(5009, "司机位置错误"),
    DRIVER_BINDING_ERROR(5010, "司机绑定关系错误"),
    DRIVER_LICENSE_ERROR(5011, "司机驾照信息错误"),
    
    // ==================== 订单相关错误码 (6000-6099) ====================
    ORDER_NOT_EXISTS(6000, "订单不存在"),
    ORDER_ALREADY_EXISTS(6001, "订单已存在"),
    ORDER_STATUS_ERROR(6002, "订单状态错误"),
    ORDER_CANNOT_CANCEL(6003, "订单无法取消"),
    ORDER_CANNOT_MODIFY(6004, "订单无法修改"),
    ORDER_ALREADY_PAID(6005, "订单已支付"),
    ORDER_NOT_PAID(6006, "订单未支付"),
    ORDER_EXPIRED(6007, "订单已过期"),
    ORDER_STATUS_NOT_ALLOW_UPDATE(6008, "订单状态不允许更新"),
    ORDER_STATUS_NOT_ALLOW_DELETE(6009, "订单状态不允许删除"),
    ORDER_AMOUNT_ERROR(6010, "订单金额错误"),
    ORDER_TIMEOUT(6011, "订单超时"),
    ORDER_DELIVERY_ERROR(6012, "订单配送错误"),
    ORDER_REFUND_ERROR(6013, "订单退款错误"),
    ORDER_MERCHANT_ERROR(6014, "订单商家信息错误"),
    ORDER_DRIVER_ERROR(6015, "订单司机信息错误"),
    ORDER_PAYMENT_ERROR(6016, "订单支付信息错误"),
    ORDER_PAYMENT_TIMEOUT(6017, "订单支付超时"),
    ORDER_STATUS_NOT_ALLOW_PAY(6018, "订单状态不允许支付"),
    ORDER_STATUS_NOT_ALLOW_CANCEL(6019, "订单状态不允许取消"),
    ORDER_STATUS_NOT_ALLOW_REFUND(6020, "订单状态不允许退款"),
    REFUND_AMOUNT_EXCEED(6021, "退款金额超出限制"),
    ORDER_STATUS_NOT_ALLOW_SHIP(6022, "订单状态不允许发货"),
    ORDER_STATUS_NOT_ALLOW_RECEIVE(6023, "订单状态不允许收货"),
    ORDER_STATUS_NOT_ALLOW_COMPLETE(6024, "订单状态不允许完成"),
    ORDER_STATUS_NOT_ALLOW_PROCESS_REFUND(6025, "订单状态不允许处理退款"),
    
    // ==================== 派单相关错误码 (7000-7099) ====================
    DISPATCH_NOT_EXISTS(7000, "派单不存在"),
    DISPATCH_ALREADY_EXISTS(7001, "派单已存在"),
    DISPATCH_STATUS_ERROR(7002, "派单状态错误"),
    DISPATCH_CANNOT_CANCEL(7003, "派单无法取消"),
    DISPATCH_DRIVER_BUSY(7004, "司机忙碌，无法派单"),
    DISPATCH_NO_AVAILABLE_DRIVER(7005, "无可用司机"),
    DISPATCH_TIMEOUT(7006, "派单超时"),
    DISPATCH_FAILED(7007, "派单失败"),
    DISPATCH_RULE_ERROR(7008, "派单规则错误"),
    DISPATCH_ALGORITHM_ERROR(7009, "派单算法错误"),
    DISPATCH_DISTANCE_TOO_FAR(7010, "派单距离过远"),
    DISPATCH_TIME_CONFLICT(7011, "派单时间冲突"),
    DISPATCH_DRIVER_ERROR(7012, "派单司机信息错误"),
    DISPATCH_ORDER_ERROR(7013, "派单订单信息错误"),
    DISPATCH_DRIVER_LOCATION_ERROR(7014, "派单司机位置信息错误"),
    DISPATCH_ORDER_LOCATION_ERROR(7015, "派单订单位置信息错误"),
    
    // ==================== 任务相关错误码 (8000-8099) ====================
    TASK_NOT_EXISTS(8000, "任务不存在"),
    TASK_ALREADY_EXISTS(8001, "任务已存在"),
    TASK_STATUS_ERROR(8002, "任务状态错误"),
    TASK_CANNOT_START(8003, "任务无法开始"),
    TASK_CANNOT_FINISH(8004, "任务无法完成"),
    TASK_TIMEOUT(8005, "任务超时"),
    TASK_CANCELLED(8006, "任务已取消"),
    TASK_FAILED(8007, "任务失败"),
    TASK_CANNOT_CANCEL(8008, "任务无法取消"),
    TASK_CANNOT_MODIFY(8009, "任务无法修改"),
    
    // ==================== 路线相关错误码 (9000-9099) ====================
    ROUTE_NOT_EXISTS(9000, "路线不存在"),
    ROUTE_ALREADY_EXISTS(9001, "路线已存在"),
    ROUTE_INVALID(9002, "路线无效"),
    ROUTE_TOO_LONG(9003, "路线过长"),
    ROUTE_CALCULATION_FAILED(9004, "路线计算失败"),
    ROUTE_OPTIMIZATION_FAILED(9005, "路线优化失败"),
    ROUTE_CALCULATION_ERROR(9006, "路线计算错误"),
    ROUTE_TIME_WINDOW_ERROR(9007, "路线时间窗口错误"),
    ROUTE_CAPACITY_EXCEEDED(9008, "路线容量超限"),
    ROUTE_TIME_EXCEEDED(9009, "路线时间超限"),
    ROUTE_DISTANCE_EXCEEDED(9010, "路线距离超限"),
    
    // ==================== 支付相关错误码 (10000-10099) ====================
    PAYMENT_FAILED(10000, "支付失败"),
    PAYMENT_TIMEOUT(10001, "支付超时"),
    PAYMENT_CANCELLED(10002, "支付已取消"),
    PAYMENT_AMOUNT_ERROR(10003, "支付金额错误"),
    PAYMENT_METHOD_ERROR(10004, "支付方式错误"),
    REFUND_FAILED(10005, "退款失败"),
    REFUND_AMOUNT_ERROR(10006, "退款金额错误"),
    PAYMENT_CHANNEL_ERROR(10007, "支付渠道错误"),
    PAYMENT_STATUS_ERROR(10008, "支付状态错误"),
    PAYMENT_ORDER_ERROR(10009, "支付订单错误"),
    PAYMENT_AMOUNT_MISMATCH(10010, "支付金额不匹配"),
    
    // ==================== 文件相关错误码 (11000-11099) ====================
    FILE_NOT_EXISTS(11000, "文件不存在"),
    FILE_UPLOAD_FAILED(11001, "文件上传失败"),
    FILE_DOWNLOAD_FAILED(11002, "文件下载失败"),
    FILE_FORMAT_ERROR(11003, "文件格式错误"),
    FILE_SIZE_EXCEEDED(11004, "文件大小超限"),
    FILE_TYPE_NOT_SUPPORTED(11005, "文件类型不支持"),
    FILE_PROCESSING_ERROR(11006, "文件处理错误"),
    FILE_READ_ERROR(11007, "文件读取错误"),
    FILE_WRITE_ERROR(11008, "文件写入错误"),
    FILE_DELETE_ERROR(11009, "文件删除错误"),
    FILE_EXISTS_ERROR(11010, "文件已存在"),
    
    // ==================== 消息相关错误码 (12000-12099) ====================
    MESSAGE_SEND_FAILED(12000, "消息发送失败"),
    MESSAGE_FORMAT_ERROR(12001, "消息格式错误"),
    SMS_SEND_FAILED(12002, "短信发送失败"),
    EMAIL_SEND_FAILED(12003, "邮件发送失败"),
    PUSH_SEND_FAILED(12004, "推送发送失败"),
    MESSAGE_TEMPLATE_ERROR(12005, "消息模板错误"),
    MESSAGE_CHANNEL_ERROR(12006, "消息渠道错误"),
    
    // ==================== 缓存相关错误码 (13000-13099) ====================
    CACHE_ERROR(13000, "缓存错误"),
    CACHE_KEY_NOT_EXISTS(13001, "缓存键不存在"),
    CACHE_EXPIRED(13002, "缓存已过期"),
    CACHE_CONNECTION_ERROR(13003, "缓存连接错误"),
    CACHE_OPERATION_ERROR(13004, "缓存操作错误"),
    
    // ==================== 配置相关错误码 (14000-14099) ====================
    CONFIG_NOT_EXISTS(14000, "配置不存在"),
    CONFIG_FORMAT_ERROR(14001, "配置格式错误"),
    CONFIG_VALUE_INVALID(14002, "配置值无效"),
    CONFIG_LOAD_FAILED(14003, "配置加载失败"),
    CONFIG_PARSE_ERROR(14004, "配置解析错误"),
    CONFIG_ERROR(14005, "配置错误"),
    
    // ==================== 租户相关错误码 (15000-15099) ====================
    TENANT_NOT_EXISTS(15000, "租户不存在"),
    TENANT_NOT_FOUND(15000, "租户不存在"),
    TENANT_DISABLED(15001, "租户已禁用"),
    TENANT_INVALID(15005, "租户无效"),
    TENANT_EXPIRED(15002, "租户已过期"),
    TENANT_QUOTA_EXCEEDED(15003, "租户配额已超限"),
    TENANT_PERMISSION_DENIED(15004, "租户权限不足"),
    TENANT_ACCESS_DENIED(15006, "租户访问被拒绝"),
    TENANT_DATA_ISOLATION_ERROR(15007, "租户数据隔离错误"),
    
    // ==================== 限流相关错误码 (16000-16099) ====================
    RATE_LIMIT_EXCEEDED(16000, "请求频率超限"),
    CONCURRENT_LIMIT_EXCEEDED(16001, "并发数超限"),
    QUOTA_EXCEEDED(16002, "配额已超限"),
    REQUEST_TOO_FREQUENT(16003, "请求过于频繁"),
    
    // ==================== 第三方服务错误码 (17000-17099) ====================
    THIRD_PARTY_SERVICE_ERROR(17000, "第三方服务错误"),
    API_CALL_FAILED(17001, "API调用失败"),
    EXTERNAL_SERVICE_TIMEOUT(17002, "外部服务超时"),
    EXTERNAL_SERVICE_UNAVAILABLE(17003, "外部服务不可用"),
    EXTERNAL_SERVICE_ERROR(17004, "外部服务错误"),
    
    // ==================== 数据相关错误码 (18000-18099) ====================
    DATA_NOT_EXISTS(18000, "数据不存在"),
    DATA_NOT_FOUND(18000, "数据不存在"),
    DATA_ALREADY_EXISTS(18001, "数据已存在"),
    DATA_FORMAT_ERROR(18002, "数据格式错误"),
    DATA_INTEGRITY_ERROR(18003, "数据完整性错误"),
    DATA_VALIDATION_FAILED(18004, "数据校验失败"),
    DATA_IMPORT_FAILED(18005, "数据导入失败"),
    DATA_EXPORT_FAILED(18006, "数据导出失败"),
    DATA_INVALID(18007, "数据无效"),
    DATA_IN_USE(18008, "数据正在使用中，无法删除"),
    
    // ==================== 业务流程错误码 (19000-19099) ====================
    WORKFLOW_ERROR(19000, "业务流程错误"),
    STEP_NOT_ALLOWED(19001, "步骤不允许"),
    CONDITION_NOT_MET(19002, "条件不满足"),
    APPROVAL_REQUIRED(19003, "需要审批"),
    APPROVAL_REJECTED(19004, "审批被拒绝"),
    WORKFLOW_TIMEOUT(19005, "流程超时"),
    INVALID_OPERATION(19006, "无效操作"),
    CONCURRENT_UPDATE(19007, "数据已被其他用户修改，请刷新后重试"),
    
    // ==================== 发票相关错误码 (20000-20099) ====================
    INVOICE_NOT_EXISTS(20000, "发票不存在"),
    INVOICE_ALREADY_EXISTS(20001, "发票已存在"),
    INVOICE_STATUS_ERROR(20002, "发票状态错误"),
    INVOICE_CANNOT_CANCEL(20003, "发票无法取消"),
    INVOICE_AMOUNT_ERROR(20004, "发票金额错误"),
    INVOICE_GENERATE_FAILED(20005, "发票生成失败"),
    INVOICE_ALREADY_GENERATED(20006, "发票已生成"),
    INVOICE_GENERATION_FAILED(20007, "发票生成失败"),
    INVOICE_PRINT_FAILED(20008, "发票打印失败"),
    
    // ==================== 统计分析错误码 (21000-21099) ====================
    STATISTICS_ERROR(21000, "统计错误"),
    REPORT_GENERATE_FAILED(21001, "报表生成失败"),
    DATA_ANALYSIS_FAILED(21002, "数据分析失败"),
    STATISTICS_CALCULATION_ERROR(21003, "统计计算错误"),
    
    // ==================== 地理位置错误码 (22000-22099) ====================
    LOCATION_ERROR(22000, "位置错误"),
    LOCATION_NOT_FOUND(22001, "位置未找到"),
    LOCATION_SERVICE_ERROR(22002, "位置服务错误"),
    GEOCODING_FAILED(22003, "地理编码失败"),
    LOCATION_INVALID(22004, "位置无效"),
    
    // ==================== 通知相关错误码 (23000-23099) ====================
    NOTIFICATION_SEND_FAILED(23000, "通知发送失败"),
    NOTIFICATION_TEMPLATE_ERROR(23001, "通知模板错误"),
    NOTIFICATION_CHANNEL_ERROR(23002, "通知渠道错误"),
    NOTIFICATION_FORMAT_ERROR(23003, "通知格式错误");
    



    /**
     * 错误码
     */
    private final Integer code;
    
    /**
     * 错误消息
     */
    private final String message;
    
    /**
     * 构造函数
     * 
     * @param code 错误码
     * @param message 错误消息
     */
    ErrorCode(Integer code, String message) {
        this.code = code;
        this.message = message;
    }
    
    /**
     * 获取错误码
     * 
     * @return 错误码
     */
    public Integer getCode() {
        return code;
    }
    
    /**
     * 获取错误消息
     * 
     * @return 错误消息
     */
    public String getMessage() {
        return message;
    }
    
    /**
     * 根据错误码获取枚举
     * 
     * @param code 错误码
     * @return 错误码枚举
     */
    public static ErrorCode getByCode(Integer code) {
        for (ErrorCode errorCode : ErrorCode.values()) {
            if (errorCode.getCode().equals(code)) {
                return errorCode;
            }
        }
        return null;
    }
    
    /**
     * 判断是否为成功码
     * 
     * @return 是否成功
     */
    public boolean isSuccess() {
        return SUCCESS.equals(this);
    }
    
    /**
     * 判断是否为客户端错误
     * 
     * @return 是否为客户端错误
     */
    public boolean isClientError() {
        return code >= 400 && code < 500;
    }
    
    /**
     * 判断是否为服务端错误
     * 
     * @return 是否为服务端错误
     */
    public boolean isServerError() {
        return code >= 500 && code < 600;
    }
    
    @Override
    public String toString() {
        return "ErrorCode{" +
                "code=" + code +
                ", message='" + message + '\'' +
                '}';
    }
}